

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Vincent">
  <meta name="keywords" content="">
  
    <meta name="description" content="第二章 ElasticSearch 入门2.1 ElasticSearch 安装2.1.1 下载软件​		Elasticsearch 的官方地址：https:&#x2F;&#x2F;www.elastic.co&#x2F;cn&#x2F; ​		Elasticsearch 分为 Linux 和 Windows 版本，基于我们主要学习的是 Elasticsearch 的 Java 客户端的使用，使用的是安装较为简便的 Windows 版本">
<meta property="og:type" content="article">
<meta property="og:title" content="第二章 ElasticSearch 入门">
<meta property="og:url" content="https://zqystudy.gitee.io/blog/2022/11/25/%E7%AC%AC%E4%BA%8C%E7%AB%A0%20ElasticSearch%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="Vincent">
<meta property="og:description" content="第二章 ElasticSearch 入门2.1 ElasticSearch 安装2.1.1 下载软件​		Elasticsearch 的官方地址：https:&#x2F;&#x2F;www.elastic.co&#x2F;cn&#x2F; ​		Elasticsearch 分为 Linux 和 Windows 版本，基于我们主要学习的是 Elasticsearch 的 Java 客户端的使用，使用的是安装较为简便的 Windows 版本">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://zqystudy.gitee.io/blog/img/ElasticSearch_logo.png">
<meta property="article:published_time" content="2022-11-25T02:11:30.000Z">
<meta property="article:modified_time" content="2022-11-25T06:44:06.626Z">
<meta property="article:author" content="Vincent">
<meta property="article:tag" content="Elasticsearch">
<meta property="article:tag" content="搜索引擎">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://zqystudy.gitee.io/blog/img/ElasticSearch_logo.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>第二章 ElasticSearch 入门 - Vincent</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/blog/css/mac.css">
<link rel="stylesheet" href="/blog/css/custom.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"zqystudy.gitee.io","root":"/blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"zc2OPBTVjL4nIWcxAdu9fkZp-gzGzoHsz","app_key":"vpIXqlueEoj5yt2RfIa84WzG","server_url":"https://zc2opbtv.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":false},"statistics":{"enable":true,"source":"leancloud","pv_format":"总访问量 {} 次","uv_format":"总访客数 {} 人"}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>VincentのBlog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/talking/">
                <i class="iconfont icon-kakao-talk-fill"></i>
                talking
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/banner.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="第二章 ElasticSearch 入门"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Vincent
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-11-25 10:11" pubdate>
          2022年11月25日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          39k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          330 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span>次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
    
    
    
    <div class="category row nomargin-x">
      <a class="category-item 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="学习笔记"
        id="heading-078425eaf316a180b0989442e53f920b" role="tab" data-toggle="collapse" href="#collapse-078425eaf316a180b0989442e53f920b"
        aria-expanded="true"
      >
        学习笔记
        <span class="list-group-count">(5)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-078425eaf316a180b0989442e53f920b"
           role="tabpanel" aria-labelledby="heading-078425eaf316a180b0989442e53f920b">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/blog/2022/08/09/ElasticSearch/" title="ElasticSearch"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">ElasticSearch</span>
        </a>
      
    
      
      
        <a href="/blog/2022/11/03/Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="Lambda表达式"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">Lambda表达式</span>
        </a>
      
    
      
      
        <a href="/blog/2022/11/03/%E5%86%85%E9%83%A8%E7%B1%BB/" title="内部类"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">内部类</span>
        </a>
      
    
      
      
        <a href="/blog/2022/10/18/%E7%AC%AC%E4%B8%80%E7%AB%A0%20Elasticsearch%E6%A6%82%E8%BF%B0/" title="第一章 Elasticsearch概述"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">第一章 Elasticsearch概述</span>
        </a>
      
    
      
      
        <a href="/blog/2022/11/25/%E7%AC%AC%E4%BA%8C%E7%AB%A0%20ElasticSearch%E5%85%A5%E9%97%A8/" title="第二章 ElasticSearch 入门"
           class="list-group-item list-group-item-action
           active">
          <span class="category-post">第二章 ElasticSearch 入门</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
    
    
    
    <div class="category row nomargin-x">
      <a class="category-item 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="必备技能"
        id="heading-2aa2150a1200e5850e48475f06ee82be" role="tab" data-toggle="collapse" href="#collapse-2aa2150a1200e5850e48475f06ee82be"
        aria-expanded="true"
      >
        必备技能
        <span class="list-group-count">(2)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-2aa2150a1200e5850e48475f06ee82be"
           role="tabpanel" aria-labelledby="heading-2aa2150a1200e5850e48475f06ee82be">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/blog/2022/10/18/%E7%AC%AC%E4%B8%80%E7%AB%A0%20Elasticsearch%E6%A6%82%E8%BF%B0/" title="第一章 Elasticsearch概述"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">第一章 Elasticsearch概述</span>
        </a>
      
    
      
      
        <a href="/blog/2022/11/25/%E7%AC%AC%E4%BA%8C%E7%AB%A0%20ElasticSearch%E5%85%A5%E9%97%A8/" title="第二章 ElasticSearch 入门"
           class="list-group-item list-group-item-action
           active">
          <span class="category-post">第二章 ElasticSearch 入门</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">第二章 ElasticSearch 入门</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2022年11月25日 下午
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="第二章-ElasticSearch-入门"><a href="#第二章-ElasticSearch-入门" class="headerlink" title="第二章 ElasticSearch 入门"></a>第二章 ElasticSearch 入门</h1><h2 id="2-1-ElasticSearch-安装"><a href="#2-1-ElasticSearch-安装" class="headerlink" title="2.1 ElasticSearch 安装"></a>2.1 ElasticSearch 安装</h2><h3 id="2-1-1-下载软件"><a href="#2-1-1-下载软件" class="headerlink" title="2.1.1 下载软件"></a>2.1.1 下载软件</h3><p>​		Elasticsearch 的官方地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/">https://www.elastic.co/cn/</a></p>
<p>​		Elasticsearch 分为 Linux 和 Windows 版本，基于我们主要学习的是 Elasticsearch 的 Java 客户端的使用，使用的是安装较为简便的 Windows 版本。本次选择的是7.8.0 版本</p>
<p>​		下载地址：：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/past-releases#elasticsearch">https://www.elastic.co/cn/downloads/past-releases#elasticsearch</a></p>
<h3 id="2-1-2-安装软件"><a href="#2-1-2-安装软件" class="headerlink" title="2.1.2 安装软件"></a>2.1.2 安装软件</h3><p>​		Windows 版的 Elasticsearch 的安装很简单，解压即安装完毕，解压后的 Elasticsearch 的 目录结构如下，由于我的电脑已经使用过Elasticsearch，所以目录结构比初始的多了个data目录，log中也多了点日志文件。</p>
<p><img src="https://img-blog.csdnimg.cn/848f9eef81894ea99257d2e59d446884.png" srcset="/blog/img/loading.gif" lazyload></p>
<table>
<thead>
<tr>
<th align="center">目录</th>
<th align="center">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="center">bin</td>
<td align="center">可执行脚本目录</td>
</tr>
<tr>
<td align="center">config</td>
<td align="center">配置目录</td>
</tr>
<tr>
<td align="center">jdk</td>
<td align="center">内置JDK目录</td>
</tr>
<tr>
<td align="center">lib</td>
<td align="center">类库</td>
</tr>
<tr>
<td align="center">logs</td>
<td align="center">日志目录</td>
</tr>
<tr>
<td align="center">modules</td>
<td align="center">模块目录</td>
</tr>
<tr>
<td align="center">plugin</td>
<td align="center">插件目录</td>
</tr>
</tbody></table>
<p><strong>解压后，进入 bin 文件目录，点击 elasticsearch.bat 文件启动 ES 服务</strong></p>
<p><img src="https://img-blog.csdnimg.cn/163bc77ea2c649e7a64afd5e5cae72d1.png" srcset="/blog/img/loading.gif" lazyload></p>
<p><strong>注意：9300端口为ElasticSearch集群间的通信端口。9200端口为浏览器访问的http协议RESTful端口。</strong></p>
<p>​	打开浏览器（推荐使用谷歌浏览器），输入地址：<a href="http://localhost:9200，测试结果">http://localhost:9200，测试结果</a></p>
<p><img src="https://img-blog.csdnimg.cn/b8a13c7be0764e398bdec5e59806c7da.png" srcset="/blog/img/loading.gif" lazyload></p>
<h3 id="2-1-3-问题解决"><a href="#2-1-3-问题解决" class="headerlink" title="2.1.3 问题解决"></a>2.1.3 问题解决</h3><ul>
<li>Elasticsearch 是使用 java 开发的，且 7.8 版本的 ES 需要 JDK 版本 1.8 以上，默认安装 包带有 jdk 环境，如果系统配置 JAVA_HOME，那么使用系统默认的 JDK，如果没有配置使用自带的 JDK，一般建议使用系统配置的 JDK。</li>
<li>双击启动窗口闪退，通过路径访问追踪错误，如果是“空间不足”，请修改 config&#x2F;jvm.options 配置文件</li>
</ul>
<figure class="highlight ldif"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs ldif"><span class="hljs-comment"># 设置 JVM 初始内存为 1G。此值可以设置与-Xmx 相同，以避免每次垃圾回收完成后 JVM 重新分配内存</span><br><span class="hljs-comment"># Xms represents the initial size of total heap space</span><br><span class="hljs-comment"># 设置 JVM 最大可用内存为 1G</span><br><span class="hljs-comment"># Xmx represents the maximum size of total heap space</span><br><br><span class="hljs-literal">-</span>Xms1g<br><span class="hljs-literal">-</span>Xmx1g<br><br></code></pre></td></tr></table></figure>

<h2 id="2-2-Elasticsearch-基本操作"><a href="#2-2-Elasticsearch-基本操作" class="headerlink" title="2.2 Elasticsearch 基本操作"></a>2.2 Elasticsearch 基本操作</h2><h3 id="2-2-1-RESTful"><a href="#2-2-1-RESTful" class="headerlink" title="2.2.1 RESTful"></a>2.2.1 RESTful</h3><p>​		<strong>REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就 是 RESTful。</strong>Web 应用程序最重要的 REST 原则是，客户端和服务器之间的交互在请求之 间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。如果服务器在请求之间的任何时间点重启，客户端不会得到通知。此外，无状态请求可以由任何可用服务器回答，这<strong>十分适合云计算之类的环境</strong>。客户端可以缓存数据以改进性能。</p>
<p>​		在服务器端，应用程序状态和功能可以分为各种资源。资源是一个有趣的概念实体，它向客户端公开。资源的例子有：应用程序对象、数据库记录、算法等等。每个资源都使用 URI  (Universal Resource Identifier) 得到一个唯一的地址。所有资源都共享统一的接口，以便在客户端和服务器之间传输状态。使用的是标准的 HTTP 方法，比如 GET、PUT、POST 和 DELETE。</p>
<p>​		 在 REST 样式的 Web 服务中，每个资源都有一个地址。资源本身都是方法调用的目标，方法列表对所有资源都是一样的。这些方法都是标准方法，包括 HTTP GET、POST、 PUT、DELETE，还可能包括 HEAD 和 OPTIONS。简单的理解就是，如果想要访问互联网上的资源，就必须向资源所在的服务器发出请求，请求体中必须包含资源的网络路径，以及对资源进行的操作(增删改查)。</p>
<h3 id="2-2-2-客户端安装"><a href="#2-2-2-客户端安装" class="headerlink" title="2.2.2 客户端安装"></a>2.2.2 客户端安装</h3><p>​		如果直接通过浏览器向 Elasticsearch 服务器发请求，那么需要在发送的请求中包含 HTTP 标准的方法，而 HTTP 的大部分特性且仅支持 GET 和 POST 方法。所以为了能方便地进行客户端的访问，可以使用 Postman 软件 </p>
<p>​		Postman 是一款强大的网页调试工具，提供功能强大的 Web API 和 HTTP 请求调试。 软件功能强大，界面简洁明晰、操作方便快捷，设计得很人性化。Postman 中文版能够发送 任何类型的 HTTP 请求 (GET, HEAD, POST, PUT..)，不仅能够表单提交，且可以附带任意类型请求体。</p>
<p>​		Postman 官网：<a target="_blank" rel="noopener" href="https://www.getpostman.com/">https://www.getpostman.com</a> </p>
<p>​		Postman 下载：<a target="_blank" rel="noopener" href="https://www.getpostman.com/apps">https://www.getpostman.com/apps</a></p>
<h3 id="2-2-3-数据格式"><a href="#2-2-3-数据格式" class="headerlink" title="2.2.3 数据格式"></a>2.2.3 数据格式</h3><p>​		Elasticsearch 是<strong>面向文档型数据库</strong>，一条数据在这里就是一个文档。为了方便大家理解， 我们将 Elasticsearch 里存储文档数据和关系型数据库 MySQL 存储数据的概念进行一个类比</p>
<p><img src="https://img-blog.csdnimg.cn/e1f48b9d4113491d8b3317081a94ce26.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>ES 里的 Index 可以看做一个库，而 Types 相当于表，Documents 则相当于表的行。 这里 Types 的概念已经被逐渐弱化，Elasticsearch 6.X 中，一个 index 下已经只能包含一个 type，Elasticsearch 7.X 中, Type 的概念已经被删除了。</p>
<pre><code class="hljs">     用 JSON 作为文档序列化的格式，比如一条用户信息：
</code></pre>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;name&quot;</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;John&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;sex&quot;</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Male&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;age&quot;</span> <span class="hljs-punctuation">:</span> <span class="hljs-number">25</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;birthDate&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1990/05/01&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;about&quot;</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;I love to go rock climbing&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;interests&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> <span class="hljs-string">&quot;sports&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;music&quot;</span> <span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h3 id="2-2-4-HTTP-操作"><a href="#2-2-4-HTTP-操作" class="headerlink" title="2.2.4 HTTP 操作"></a>2.2.4 HTTP 操作</h3><h4 id="2-2-4-1-索引操作"><a href="#2-2-4-1-索引操作" class="headerlink" title="2.2.4.1 索引操作"></a>2.2.4.1 索引操作</h4><h5 id="1-创建索引"><a href="#1-创建索引" class="headerlink" title="1.创建索引"></a>1.创建索引</h5><p>对比关系型数据库，创建索引就等同于创建数据库 在 Postman 中，向 ES 服务器发 <strong>PUT</strong> 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p><img src="https://img-blog.csdnimg.cn/bda7be2a6df84f00bc06a52d8c2880dc.png" srcset="/blog/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>请求后，服务器返回响应</p>
<p><img src="https://img-blog.csdnimg.cn/456037951caa4dcaacb529393ab27df3.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-string">&quot;acknowledged&quot;</span>【响应结果】<span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span> # <span class="hljs-literal"><span class="hljs-keyword">true</span></span> 操作成功<br> <span class="hljs-string">&quot;shards_acknowledged&quot;</span>【分片结果】<span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span> # 分片操作成功<br> <span class="hljs-string">&quot;index&quot;</span>【索引名称】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><br><span class="hljs-punctuation">&#125;</span><br># 注意：创建索引库的分片数默认 <span class="hljs-number">1</span> 片，在 <span class="hljs-number">7.0</span><span class="hljs-number">.0</span> 之前的 Elasticsearch 版本中，默认 <span class="hljs-number">5</span> 片<br></code></pre></td></tr></table></figure>

<p>如果重复添加索引，会返回错误信息</p>
<p><img src="https://img-blog.csdnimg.cn/f4596cb247b842e1a2155a87d478e9d3.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="2-查询所有索引"><a href="#2-查询所有索引" class="headerlink" title="2.查询所有索引"></a>2.查询所有索引</h5><p>在 Postman 中，向 ES 服务器发 GET 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/_cat/indices?v">http://127.0.0.1:9200/_cat/indices?v</a></p>
<p><img src="https://img-blog.csdnimg.cn/6604176623394e03870e715da4723733.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>这里请求路径中的**_ca<strong>t 表示查看的意思，</strong>indices** 表示索引，所以整体含义就是查看当前 ES 服务器中的所有索引，就好像 MySQL 中的 show tables 的感觉，服务器响应结果如下</p>
<p><img src="https://img-blog.csdnimg.cn/cb77f3a7b7be4c52b61625571d6147cf.png" srcset="/blog/img/loading.gif" lazyload></p>
<table>
<thead>
<tr>
<th align="center">表头</th>
<th align="center">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="center">health</td>
<td align="center">当前服务器健康状态：$\textcolor{green}{green}$（集群完整） $\textcolor{yellow}{yellow}$(单点正常，集群不完整) $\textcolor{red}{red}$（单点不正常）</td>
</tr>
<tr>
<td align="center">status</td>
<td align="center">索引打开、关闭状态</td>
</tr>
<tr>
<td align="center">index</td>
<td align="center">索引名</td>
</tr>
<tr>
<td align="center">uuid</td>
<td align="center">索引统一编号</td>
</tr>
<tr>
<td align="center">pri</td>
<td align="center">主分片数量</td>
</tr>
<tr>
<td align="center">rep</td>
<td align="center">副本数量</td>
</tr>
<tr>
<td align="center">docs.count</td>
<td align="center">可用文档数量</td>
</tr>
<tr>
<td align="center">docs.deleted</td>
<td align="center">文档删除状态（逻辑删除）</td>
</tr>
<tr>
<td align="center">store.size</td>
<td align="center">主分片和副分片整体占空间大小</td>
</tr>
<tr>
<td align="center">pri.store.size</td>
<td align="center">主分片占空间大小</td>
</tr>
</tbody></table>
<h5 id="3-查看单个索引"><a href="#3-查看单个索引" class="headerlink" title="3.查看单个索引"></a>3.查看单个索引</h5><p>在 Postman 中，向 ES 服务器发 GET 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p><img src="https://img-blog.csdnimg.cn/fb2e734a4a5b4b0da6004e452adc68bc.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>查看索引向 ES 服务器发送的请求路径和创建索引是一致的。但是 HTTP 方法不一致。这里 可以体会一下 RESTful 的意义， 请求后，服务器响应结果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/305a1e2da6674d22808791065590b062.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-string">&quot;shopping&quot;</span>【索引名】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-string">&quot;aliases&quot;</span>【别名】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;mappings&quot;</span>【映射】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;settings&quot;</span>【设置】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-string">&quot;index&quot;</span>【设置-索引】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-string">&quot;creation_date&quot;</span>【设置-索引-创建时间】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1659926095568&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-string">&quot;number_of_shards&quot;</span>【设置 - 索引 - 主分片数量】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-string">&quot;number_of_replicas&quot;</span>【设置 - 索引 - 副分片数量】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-string">&quot;uuid&quot;</span>【设置 - 索引 - 唯一标识】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;99pOhKG0RDi-Vk7ezUKIdQ&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-string">&quot;version&quot;</span>【设置 - 索引 - 版本】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;created&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;7080099&quot;</span><br>                <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-string">&quot;provided_name&quot;</span>【设置 - 索引 - 名称】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">&#125;</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="4-删除索引"><a href="#4-删除索引" class="headerlink" title="4.删除索引"></a>4.删除索引</h5><p>在 Postman 中，向 ES 服务器发 DELETE 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping">http://127.0.0.1:9200/shopping</a></p>
<p><img src="https://img-blog.csdnimg.cn/271261667d3e4d05b5089adf2ef8df93.png" srcset="/blog/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/b73fde669f074a918c48cb098ae6779e.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>重新访问索引时，服务器返回响应：索引不存在</p>
<p><img src="https://img-blog.csdnimg.cn/2bdc2abb998f453fb993a4c4644febde.png" srcset="/blog/img/loading.gif" lazyload></p>
<h4 id="2-2-4-2-文档操作"><a href="#2-2-4-2-文档操作" class="headerlink" title="2.2.4.2 文档操作"></a>2.2.4.2 文档操作</h4><h5 id="1-创建文档"><a href="#1-创建文档" class="headerlink" title="1.创建文档"></a>1.创建文档</h5><p>索引已经创建好了，接下来我们来创建文档，并添加数据。这里的文档可以类比为关系型数据库中的表数据，添加的数据格式为 <strong>JSON</strong> 格式</p>
<p>在 Postman 中，向 ES 服务器发 POST 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc">http://127.0.0.1:9200/shopping/_doc</a></p>
<p>请求体内容为：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小米手机&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;category&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小米&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;images&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;http://www.gulixueyuan.com/xm.jpg&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">3999.00</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/945fbb4a3e2d44889e74ba40165cc948.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>此处发送请求的方式必须为POST，不能时PUT，否则会发生错误</p>
<p><img src="https://img-blog.csdnimg.cn/def6320d3d584c76a6641c64672f1203.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>服务器响应结果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/022536b1e79341edbab2902523981023.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-string">&quot;_index&quot;</span>【索引】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;_type&quot;</span>【类型-文档】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;_id&quot;</span>【唯一标识】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;7s__e4IBCAQrMMvidOjT&quot;</span><span class="hljs-punctuation">,</span> #可以类比为MySQL中的主键，随机生成<br>    <span class="hljs-string">&quot;_version&quot;</span>【版本】<span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;result&quot;</span>【结果】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;created&quot;</span><span class="hljs-punctuation">,</span> #这里的create表示创建成果<br>    <span class="hljs-string">&quot;_shards&quot;</span>【分片】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-string">&quot;total&quot;</span>【分片-总数】<span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;successful&quot;</span>【分片-成功】<span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;failed&quot;</span>【分片-失败】<span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_seq_no&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_primary_term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p>上面的数据创建后，由于没有指定数据唯一性标识（ID），默认情况下，ES 服务器会随机 生成一个。</p>
<p>如果想要自定义唯一性标识，需要在创建时指定：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a></p>
<p><img src="https://img-blog.csdnimg.cn/ff3d49594d1c4c49a06b5580169ed457.png" srcset="/blog/img/loading.gif" lazyload></p>
<p><strong>$\textcolor{red}{此处需要注意，如果增加数据时明确数据主键，那么请求方式也可以为PUT}$</strong></p>
<h5 id="2-查看文档"><a href="#2-查看文档" class="headerlink" title="2.查看文档"></a>2.查看文档</h5><p>查看文档时，需要指明文档的唯一性标识，类似于 MySQL 中数据的主键查询 在 Postman 中，向 ES 服务器发 GET 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a></p>
<p><img src="https://img-blog.csdnimg.cn/393aaa8137454cd680ea0e0fa58630e3.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>查询成功后，服务器响应结果</p>
<p><img src="https://img-blog.csdnimg.cn/5de35a5007cb447faf1410ecba830084.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-string">&quot;_index&quot;</span>【索引】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;_type&quot;</span>【类型-文档】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_seq_no&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_primary_term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;found&quot;</span>【查看结果】<span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span> #<span class="hljs-literal"><span class="hljs-keyword">true</span></span>表示查找到，<span class="hljs-literal"><span class="hljs-keyword">false</span></span>表示未找到<br>    <span class="hljs-string">&quot;_source&quot;</span>【文档源信息】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小米手机&quot;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;category&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小米&quot;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;images&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;http://www.gulixueyuan.com/xm.jpg&quot;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">3999.00</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-修改文档"><a href="#3-修改文档" class="headerlink" title="3.修改文档"></a>3.修改文档</h5><p>和新增文档一样，输入相同的 URL 地址请求，如果请求体变化，会将原有的数据内容覆盖 在 Postman 中，向 ES 服务器发 POST 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a> </p>
<p>请求体内容为:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;华为手机&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;category&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;华为&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;images&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;http://www.gulixueyuan.com/hw.jpg&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">4999.00</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/e3532d75c5074d4dab42cd642fec4350.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>修改成功后，服务器响应结果</p>
<p><img src="https://img-blog.csdnimg.cn/f804c2d2e18149408f13362dd06ea4be.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;result&quot;</span>【结果】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;updated&quot;</span><span class="hljs-punctuation">,</span>  #updated表示数据被更新<br>    <span class="hljs-attr">&quot;_shards&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;successful&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;failed&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_seq_no&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_primary_term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="4-修改字段"><a href="#4-修改字段" class="headerlink" title="4.修改字段"></a>4.修改字段</h5><p>修改数据时，也可以只修改某一给条数据的局部信息</p>
<p>在 Postman 中，向 ES 服务器发 POST 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_update/1">http://127.0.0.1:9200/shopping/_update/1</a> 请求体内容为：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span> <br> 	<span class="hljs-attr">&quot;doc&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br> 		<span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">3000.00</span><br> 	<span class="hljs-punctuation">&#125;</span> <br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/720e27aa459247e0a845b400801bd884.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>修改成功后，服务器返回结果</p>
<p><img src="https://img-blog.csdnimg.cn/38943b408a274438abe39d275d55dd27.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>根据唯一标识，查询文档数据，文档数据已更新</p>
<p><img src="https://img-blog.csdnimg.cn/9e4b98230a664f308d13a126ffd67dda.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="5-删除文档"><a href="#5-删除文档" class="headerlink" title="5.删除文档"></a>5.删除文档</h5><p>删除一个文档不会立即从磁盘上移除，它只是被标记成已删除（逻辑删除）。 在 Postman 中，向 ES 服务器发 DELETE 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_doc/1">http://127.0.0.1:9200/shopping/_doc/1</a></p>
<p><img src="https://img-blog.csdnimg.cn/a4f5346212c34fea92f49ab2af52216a.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>删除成功，服务器响应结果：</p>
<p><img src="https://img-blog.csdnimg.cn/8171866ea80449a9a90bb53324ce7b66.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;_version&quot;</span>【版本】<span class="hljs-punctuation">:</span> <span class="hljs-number">4</span><span class="hljs-punctuation">,</span> #对数据的操作，都会更新版本<br>    <span class="hljs-string">&quot;result&quot;</span>【结果】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;deleted&quot;</span><span class="hljs-punctuation">,</span> #deleted表示数据被标记为删除<br>    <span class="hljs-attr">&quot;_shards&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;successful&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;failed&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_seq_no&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">4</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_primary_term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p>删除后再查询当前文档信息</p>
<p><img src="https://img-blog.csdnimg.cn/2e4a030a77ad4a479ef4420d28702bc2.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>如果删除一个并不存在的文档</p>
<p><img src="https://img-blog.csdnimg.cn/6f6fe0db3dd345a0be06e451024ca33e.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;shopping&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;result&quot;</span>【结果】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;not_found&quot;</span><span class="hljs-punctuation">,</span> #表示未查找到<br>    <span class="hljs-attr">&quot;_shards&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;successful&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;failed&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_seq_no&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_primary_term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="6-条件删除文档"><a href="#6-条件删除文档" class="headerlink" title="6.条件删除文档"></a>6.条件删除文档</h5><p>一般删除数据都是根据文档的唯一性标识进行删除，实际操作时，也可以根据条件对多条数 据进行删除</p>
<p>首先分别增加多条数据:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小米手机&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;category&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;小米&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;images&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;http://www.gulixueyuan.com/xm.jpg&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">4000.00</span><br><span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;华为手机&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;category&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;华为&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;images&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;http://www.gulixueyuan.com/hw.jpg&quot;</span><span class="hljs-punctuation">,</span><br> <span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">4000.00</span><br><span class="hljs-punctuation">&#125;</span><br><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/86e63704d62a48eabf984a6e4aae355b.png" srcset="/blog/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/9caba4124f354ce2a69bb805a0db3d74.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>向 ES 服务器发 POST 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/shopping/_delete_by_query">http://127.0.0.1:9200/shopping/_delete_by_query</a></p>
<p>请求体内容为：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> 	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br> 		<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br> 			<span class="hljs-attr">&quot;price&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">4000.00</span><br> 		 <span class="hljs-punctuation">&#125;</span><br> 	 <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/ec32449c2ae545499ceeb6ce9d532cc2.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>删除成功后，服务器响应结果：</p>
<p><img src="https://img-blog.csdnimg.cn/e5ecd3aa432645ce9dbfe9afd67f3ad9.png" srcset="/blog/img/loading.gif" lazyload></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-string">&quot;took&quot;</span>【耗时】<span class="hljs-punctuation">:</span> <span class="hljs-number">84</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;timed_out&quot;</span>【是否超时】<span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;total&quot;</span>【总数】<span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;deleted&quot;</span>【删除数量】<span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;batches&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;version_conflicts&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;noops&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;retries&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;bulk&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;search&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;throttled_millis&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;requests_per_second&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">-1.0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;throttled_until_millis&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;failures&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h4 id="2-2-4-3-映射操作"><a href="#2-2-4-3-映射操作" class="headerlink" title="2.2.4.3 映射操作"></a>2.2.4.3 映射操作</h4><p>​		有了索引库，等于有了数据库中的 database。</p>
<p>​		接下来就需要建索引库(index)中的映射了，**类似于数据库(database)中的表结构(table)**。 创建数据库表需要设置字段名称，类型，长度，约束等；索引库也一样，需要知道这个类型 下有哪些字段，每个字段有哪些约束信息，这就叫做映射(mapping)。</p>
<h5 id="1-创建映射"><a href="#1-创建映射" class="headerlink" title="1.创建映射"></a>1.创建映射</h5><p>在 Postman 中，向 ES 服务器发 PUT 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/student/_mapping">http://127.0.0.1:9200/student/_mapping</a> </p>
<p>请求体内容为：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;properties&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;text&quot;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><br>		<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;text&quot;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><br>		<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;long&quot;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/a8c9fe57b0a549ffb7dfa477e82f607e.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>服务器响应结果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/3ce51e12507c4b868c8a3bcebea47061.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>映射数据说明：</p>
<ul>
<li><p>字段名：任意填写，下面指定许多属性，例如：title，subtitle，images，price</p>
</li>
<li><p>type：类型，ElasticSearch中支持的数据类型非常丰富，说几个关键的</p>
<ul>
<li><p>String类型，又分两种</p>
<p>text：可分词</p>
<p>keyword：不可分词，数据会作为完整字段进行匹配</p>
</li>
<li><p>Numerical：数值类型，分两种</p>
<p>基本数据类型：long、integer、short、byte、double、float、half_float</p>
<p>浮点数的高精度类型：scaled_float3</p>
</li>
<li><p>Date：日期类型</p>
</li>
<li><p>Array：数据类型</p>
</li>
<li><p>Object：对象</p>
</li>
</ul>
</li>
<li><p>index：是否索引，默认为true，也就是说你不进行任何配置，所有字段都会被索引。</p>
<ul>
<li>true：字段会被索引，则可以用来进行搜索</li>
<li>false：字段不会被索引，不能用来搜索</li>
</ul>
</li>
<li><p>store：是否将数据进行独立存储，，默认为false</p>
<p>原始的文本会存储在_source里面，默认情况下其他提取出来的字段都不是独立存储的，而是从source里面提取出来的。当然你也可以独立的存储某个字段，只要设置 “store”: true 即可，获取独立存储的字段要比从source 中解析快得多，但是也会占用 更多的空间，所以要根据实际业务需求来设置。</p>
</li>
<li><p>analyzer：分词器，这里的ik_max_word即使用ik分词器</p>
</li>
</ul>
<h5 id="2-查看映射"><a href="#2-查看映射" class="headerlink" title="2.查看映射"></a>2.查看映射</h5><p>在 Postman 中，向 ES 服务器发 GET 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/student/_mapping">http://127.0.0.1:9200/student/_mapping</a></p>
<p><img src="https://img-blog.csdnimg.cn/8ec278e04df242f595aabc9417b90bf4.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>服务器响应结果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/2c7ebd5f5b3d4bbfb71df857d63f5bd7.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="3-索引映射关联"><a href="#3-索引映射关联" class="headerlink" title="3. 索引映射关联"></a>3. 索引映射关联</h5><p>在 Postman 中，向 ES 服务器发 PUT 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/student1">http://127.0.0.1:9200/student1</a></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;settings&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;mappings&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;properties&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;text&quot;</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><br>			<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;text&quot;</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><br>			<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;long&quot;</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-attr">&quot;index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/752e7fcd002e4a8fbf6b0c53041abba9.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>服务器响应结果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/ac49c67599a34866b8ac9a64dedbdd8d.png" srcset="/blog/img/loading.gif" lazyload></p>
<h4 id="2-2-4-4-高级查询"><a href="#2-2-4-4-高级查询" class="headerlink" title="2.2.4.4 高级查询"></a>2.2.4.4 高级查询</h4><p>Elasticsearch 提供了基于 JSON 提供完整的查询 DSL 来定义查询 </p>
<p>以下所有查询均在postman中发送 GET 请求 ：<a target="_blank" rel="noopener" href="http://127.0.0.1:9200/student/_search">http://127.0.0.1:9200/student/_search</a></p>
<p>定义数据 :</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs json"># POST /student/_doc/<span class="hljs-number">1001</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;男&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><br><span class="hljs-punctuation">&#125;</span><br># POST /student/_doc/<span class="hljs-number">1002</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;lisi&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;lisi&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;男&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">20</span><br><span class="hljs-punctuation">&#125;</span><br># POST /student/_doc/<span class="hljs-number">1003</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;wangwu&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;wangwu&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">40</span><br><span class="hljs-punctuation">&#125;</span><br># POST /student/_doc/<span class="hljs-number">1004</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan1&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan1&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">50</span><br><span class="hljs-punctuation">&#125;</span><br># POST /student/_doc/<span class="hljs-number">1005</span><br><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan2&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan2&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><br><span class="hljs-punctuation">&#125;</span><br><br></code></pre></td></tr></table></figure>



<h5 id="1-查看所有文档"><a href="#1-查看所有文档" class="headerlink" title="1.查看所有文档"></a>1.查看所有文档</h5><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;match_all&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br># <span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span>这里的query代表一个查询对象，里面可以有不同的查询属性<br># <span class="hljs-attr">&quot;match_all&quot;</span><span class="hljs-punctuation">:</span>查询类型，例如：match_all(代表查询所有)， match，term ， range 等等，<br># <span class="hljs-punctuation">&#123;</span>查询条件<span class="hljs-punctuation">&#125;</span>：查询条件会根据类型的不同，写法也有差异<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/998b6f42d88946e4846cf871aa8c47e6.png" srcset="/blog/img/loading.gif" lazyload></p>
<p>服务器响应结果如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;took&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">246</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;timed_out&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;_shards&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;successful&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;skipped&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;failed&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;hits&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-attr">&quot;value&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-attr">&quot;relation&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;eq&quot;</span><br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;max_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;hits&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>            <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;student&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1001&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;男&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;student&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1002&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;lisi&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;lisi&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;男&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">20</span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;student&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1003&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;wangwu&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;wangwu&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">40</span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;student&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1004&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan1&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan1&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">50</span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;_index&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;student&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;_doc&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1005&quot;</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>                    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan2&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan2&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;女&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><br>                <span class="hljs-punctuation">&#125;</span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">]</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-string">&quot;took&quot;</span>【查询总花费时间】<span class="hljs-punctuation">:</span> <span class="hljs-number">246</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;timed_out&quot;</span>【是否超时】<span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;_shards&quot;</span>【分片信息】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-string">&quot;total&quot;</span>【总数】<span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;successful&quot;</span>【成功】<span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;skipped&quot;</span>【忽略】<span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;failed&quot;</span>【失败】<span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br>    <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-string">&quot;hits&quot;</span>【搜索命中结果】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-string">&quot;total&quot;</span>【搜索条件匹配的文档总数】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-string">&quot;value&quot;</span>【总命中计数的值】<span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-string">&quot;relation&quot;</span>【计数规则】<span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;eq&quot;</span>  #eq表示计数准确，get表示计数不准确<br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;max_score&quot;</span>【匹配值分值】<span class="hljs-punctuation">:</span> <span class="hljs-number">1.0</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-string">&quot;hits&quot;</span>【命中结果集合】<span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>           ....<br>        <span class="hljs-punctuation">]</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="2-匹配查询"><a href="#2-匹配查询" class="headerlink" title="2.匹配查询"></a>2.匹配查询</h5><p>match 匹配类型查询，会把查询条件进行分词，然后进行查询，多个词条之间是 or 的关系 </p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br> <span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;zhangsan&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><br>  <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>





<p><img src="https://img-blog.csdnimg.cn/b3af50fde4434eb883ebbbf65a58485e.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="3-字段匹配查询"><a href="#3-字段匹配查询" class="headerlink" title="3.字段匹配查询"></a>3.字段匹配查询</h5><p>multi_match 与 match 类似，不同的是它可以在多个字段中查询。 </p>
<figure class="highlight prolog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs prolog">&#123;<br>    <span class="hljs-string">&quot;query&quot;</span>: &#123;<br>        <span class="hljs-string">&quot;multi_match&quot;</span>: &#123;<br>            <span class="hljs-string">&quot;query&quot;</span>: <span class="hljs-string">&quot;zhangsan&quot;</span>,<br>            <span class="hljs-string">&quot;fields&quot;</span>: [<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;nickname&quot;</span>]<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h5 id="4-关键字精确查询"><a href="#4-关键字精确查询" class="headerlink" title="4.关键字精确查询"></a>4.关键字精确查询</h5><p>term 查询，精确的关键词匹配查询，不对查询条件进行分词。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;term&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;value&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="5-多关键字精确查询"><a href="#5-多关键字精确查询" class="headerlink" title="5. 多关键字精确查询"></a>5. 多关键字精确查询</h5><p>terms 查询和 term 查询一样，但它允许你指定多值进行匹配。 如果这个字段包含了指定值中的任何一个值，那么这个文档满足条件，类似于 mysql 的 in</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;terms&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-string">&quot;lisi&quot;</span><br>			<span class="hljs-punctuation">]</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="6-指定查询字段"><a href="#6-指定查询字段" class="headerlink" title="6. 指定查询字段"></a>6. 指定查询字段</h5><p>默认情况下，Elasticsearch 在搜索的结果中，会把文档中保存在source 的所有字段都返回。 如果我们只想获取其中的部分字段，我们可以添加source 的过滤</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>		<span class="hljs-string">&quot;name&quot;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-string">&quot;nickname&quot;</span><br>	<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;terms&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-string">&quot;zhangsan&quot;</span><br>			<span class="hljs-punctuation">]</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="7-过滤字段"><a href="#7-过滤字段" class="headerlink" title="7.过滤字段"></a>7.过滤字段</h5><p>我们也可以通过：</p>
<ul>
<li>includes：来指定想要显示的字段</li>
<li>excludes：来指定不想要显示的字段</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;_source&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;includes&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>			<span class="hljs-string">&quot;name&quot;</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-string">&quot;nickname&quot;</span><br>		<span class="hljs-punctuation">]</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;terms&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;nickname&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-string">&quot;zhangsan&quot;</span><br>			<span class="hljs-punctuation">]</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<h5 id="8-组合查询"><a href="#8-组合查询" class="headerlink" title="8.组合查询"></a>8.组合查询</h5><p><code>bool</code>把各种其它查询通过<code>must</code>（必须 ）、<code>must_not</code>（必须不）、<code>should</code>（应该）的方 式进行组合</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;bool&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;must&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-punctuation">&#123;</span><br>					<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>						<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><br>					<span class="hljs-punctuation">&#125;</span><br>				<span class="hljs-punctuation">&#125;</span><br>			<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;must_not&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-punctuation">&#123;</span><br>					<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>						<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;40&quot;</span><br>					<span class="hljs-punctuation">&#125;</span><br>				<span class="hljs-punctuation">&#125;</span><br>			<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>			<span class="hljs-attr">&quot;should&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>				<span class="hljs-punctuation">&#123;</span><br>					<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>						<span class="hljs-attr">&quot;sex&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;男&quot;</span><br>					<span class="hljs-punctuation">&#125;</span><br>				<span class="hljs-punctuation">&#125;</span><br>			<span class="hljs-punctuation">]</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="9-范围查询"><a href="#9-范围查询" class="headerlink" title="9.范围查询"></a>9.范围查询</h5><p>range 查询找出那些落在指定区间的数字或者事件。range查询允许以下字符</p>
<table>
<thead>
<tr>
<th align="center">操作符</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">gt</td>
<td align="center">大于 &gt;</td>
</tr>
<tr>
<td align="center">gte</td>
<td align="center">大于等于 &gt;&#x3D;</td>
</tr>
<tr>
<td align="center">lt</td>
<td align="center">小于 &lt;</td>
</tr>
<tr>
<td align="center">lte</td>
<td align="center">小于等于 &lt;&#x3D;</td>
</tr>
</tbody></table>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;range&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;gte&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">30</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-attr">&quot;lte&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">35</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="10-模糊查询"><a href="#10-模糊查询" class="headerlink" title="10.模糊查询"></a>10.模糊查询</h5><p>返回包含与搜索字词相似的字词的文档。 编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括：</p>
<ul>
<li>更改字符（box → fox）</li>
<li>删除字符（black → lack）</li>
<li>插入字符（sic → sick）</li>
<li>转置两个相邻字符（act → cat）</li>
</ul>
<p>为了找到相似的术语，fuzzy 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体 或扩展。然后查询返回每个扩展的完全匹配。</p>
<p>通过 fuzziness 修改编辑距离。一般使用默认值 AUTO，根据术语的长度生成编辑距离。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;fuzzy&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;value&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;fuzzy&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;title&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;value&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><span class="hljs-punctuation">,</span><br>				<span class="hljs-attr">&quot;fuzziness&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span>  #表示可以模糊差别两个字符<br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="11-单字段排序"><a href="#11-单字段排序" class="headerlink" title="11.单字段排序"></a>11.单字段排序</h5><p>sort 可以让我们按照不同的字段进行排序，并且通过 order 指定排序的方式。desc 降序，asc 升序。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sort&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>		<span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;order&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;desc&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="12-多字段排序"><a href="#12-多字段排序" class="headerlink" title="12.多字段排序"></a>12.多字段排序</h5><p>假定我们想要结合使用 age 和 _score 进行查询，并且匹配的结果首先按照年龄排序，然后按照相关性得分排序</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;match_all&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sort&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>		<span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;order&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;desc&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;_score&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;order&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;desc&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="13-高亮查询"><a href="#13-高亮查询" class="headerlink" title="13.高亮查询"></a>13.高亮查询</h5><p>在进行关键字搜索时，搜索出的内容中的关键字会显示不同的颜色，称之为高亮</p>
<blockquote>
<p>在百度搜索”京东</p>
</blockquote>
<p>!(<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/8672f7db2fca4396894401e05c06417a.png">https://img-blog.csdnimg.cn/8672f7db2fca4396894401e05c06417a.png</a>)</p>
<p>Elasticsearch 可以对查询内容中的关键字部分，进行标签和样式(高亮)的设置。 在使用 match 查询的同时，加上一个 highlight 属性：</p>
<ul>
<li>pre_tags：前置标签</li>
<li>post_tags：后置标签 </li>
<li>fields：需要高亮的字段</li>
<li>title：这里声明 title 字段需要高亮，后面可以为这个字段设置特有配置，也可以空</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;match&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;zhangsan&quot;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;highlight&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;pre_tags&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&lt;font color=&#x27;red&#x27;&gt;&quot;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-attr">&quot;post_tags&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&lt;/font&gt;&quot;</span><span class="hljs-punctuation">,</span><br>		<span class="hljs-attr">&quot;fields&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="14-分页查询"><a href="#14-分页查询" class="headerlink" title="14.分页查询"></a>14.分页查询</h5><p>from：当前页的起始索引，默认从 0 开始。 from &#x3D; (pageNum - 1) * size size：每页显示多少条</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;query&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;match_all&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;sort&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>		<span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;order&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;desc&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;from&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="15-聚合查询"><a href="#15-聚合查询" class="headerlink" title="15. 聚合查询"></a>15. 聚合查询</h5><p>聚合允许使用者对 es 文档进行统计分析，类似与关系型数据库中的 group by，当然还有很 多其他的聚合，例如取最大值、平均值等等。</p>
<ul>
<li>对某个字段取最大值 max</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;max_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;max&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>对某个字段取最小值 min</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;min_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;min&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>对某个字段求和 sum</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;sum_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;sum&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>对某个字段取平均值 avg</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;avg_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;avg&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>对某个字段的值进行去重之后再取总数</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;distinct_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;cardinality&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>State 聚合</li>
</ul>
<p>stats 聚合，对某个字段一次性返回 <strong>count，max，min，avg 和 sum</strong> 五个指标</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;stats_age&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;stats&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h5 id="16-桶聚合查询"><a href="#16-桶聚合查询" class="headerlink" title="16.桶聚合查询"></a>16.桶聚合查询</h5><p>桶聚和相当于 sql 中的 group by 语句</p>
<ul>
<li>terms 聚合，分组统计</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;age_groupby&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;terms&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>在 terms 分组下再进行聚合</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>	<span class="hljs-attr">&quot;aggs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>		<span class="hljs-attr">&quot;age_groupby&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>			<span class="hljs-attr">&quot;terms&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">&#123;</span><br>				<span class="hljs-attr">&quot;field&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;age&quot;</span><br>			<span class="hljs-punctuation">&#125;</span><br>		<span class="hljs-punctuation">&#125;</span><br>	<span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>	<span class="hljs-attr">&quot;size&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">0</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>



<h3 id="2-2-5-Java-API-操作"><a href="#2-2-5-Java-API-操作" class="headerlink" title="2.2.5 Java API 操作"></a>2.2.5 Java API 操作</h3><p>​		Elasticsearch 软件是由 Java 语言开发的，所以也可以通过 Java API 的方式对 Elasticsearch 服务进行访问</p>
<h4 id="2-2-5-1-创建-Maven-项目"><a href="#2-2-5-1-创建-Maven-项目" class="headerlink" title="2.2.5.1 创建 Maven 项目"></a>2.2.5.1 创建 Maven 项目</h4><p>​		在IDEA工具中创建一个maven项目，并修改pom文件，增加Maven依赖关系</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.elasticsearch<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>elasticsearch<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>7.8.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- elasticsearch 的客户端 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.elasticsearch.client<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>elasticsearch-rest-high-level-client<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>7.8.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- elasticsearch 依赖 2.x 的 log4j --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>log4j-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.8.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>log4j-core<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.8.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.fasterxml.jackson.core<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jackson-databind<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.9<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- junit 单元测试 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>4.12<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>



<h4 id="2-2-5-2-客户端对象"><a href="#2-2-5-2-客户端对象" class="headerlink" title="2.2.5.2 客户端对象"></a>2.2.5.2 客户端对象</h4><p>​		创建 Elasticsearch01_Client 类，代码中创建 Elasticsearch 客户端对象 因为早期版本的客户端对象已经不再推荐使用，且在未来版本中会被删除，所以这里我们采 用高级 REST 客户端对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Client</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">esClient</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//关闭客户端连接</span><br>        esClient.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>$\textcolor{red}{注意}$<strong>：</strong>9200</strong> 端口为 Elasticsearch 的 Web 通信端口，<strong>localhost</strong> 为启动 ES 服务的主机名执行代码，查看控制台信息：如果运行成功没有报错并自动退出，则成功</p>
<h4 id="2-2-5-3-索引操作"><a href="#2-2-5-3-索引操作" class="headerlink" title="2.2.5.3 索引操作"></a>2.2.5.3 索引操作</h4><p>​		ES 服务器正常启动后，可以通过 Java API 客户端对象对 ES 索引进行操作</p>
<h5 id="1-创建索引-1"><a href="#1-创建索引-1" class="headerlink" title="1.创建索引"></a>1.创建索引</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Index_Create</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">esClient</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//创建索引 - 请求对象</span><br>        <span class="hljs-type">CreateIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CreateIndexRequest</span>(<span class="hljs-string">&quot;users&quot;</span>);<br>        <span class="hljs-comment">//发送请求，获取响应</span><br>        <span class="hljs-type">CreateIndexResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> esClient.indices().create(request, RequestOptions.DEFAULT);<br>        <span class="hljs-comment">//获取结果</span><br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">acknowledged</span> <span class="hljs-operator">=</span> response.isAcknowledged();<br><br>        System.out.println(<span class="hljs-string">&quot;操作状态：&quot;</span>+acknowledged);<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        esClient.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/2a6597d3acb74077a7a2e8f625c4e980.png" srcset="/blog/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h5 id="2-查看索引"><a href="#2-查看索引" class="headerlink" title="2.查看索引"></a>2.查看索引</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Index_Search</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//查询索引 - 请求对象</span><br>        <span class="hljs-type">GetIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">GetIndexRequest</span>(<span class="hljs-string">&quot;users&quot;</span>);<br>        <span class="hljs-comment">//发送请求，获取响应</span><br>        <span class="hljs-type">GetIndexResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.indices().get(request, RequestOptions.DEFAULT);<br><br>        System.out.println(<span class="hljs-string">&quot;aliases：&quot;</span>+response.getAliases());<br>        System.out.println(<span class="hljs-string">&quot;mappings：&quot;</span>+response.getMappings());<br>        System.out.println(<span class="hljs-string">&quot;settings：&quot;</span>+response.getSettings());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/c1cab7f6640b425cb33b523d36d83c1c.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="3-删除索引"><a href="#3-删除索引" class="headerlink" title="3.删除索引"></a>3.删除索引</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Index_Delete</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//删除索引 - 请求对象</span><br>        <span class="hljs-type">DeleteIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteIndexRequest</span>(<span class="hljs-string">&quot;user&quot;</span>);<br>        <span class="hljs-comment">//发送请求，获取响应</span><br>        <span class="hljs-type">AcknowledgedResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.indices().delete(request, RequestOptions.DEFAULT);<br><br>        System.out.println(<span class="hljs-string">&quot;操作结果：&quot;</span>+response.isAcknowledged());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/8d220368e9194e419b2b06f5baa8ec51.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="2-2-5-4-文档操作"><a href="#2-2-5-4-文档操作" class="headerlink" title="2.2.5.4 文档操作"></a>2.2.5.4 文档操作</h5><h5 id="1-新增文档"><a href="#1-新增文档" class="headerlink" title="1.新增文档"></a>1.新增文档</h5><p>创建数据模型</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.vincent;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> Integer age;<br>    <span class="hljs-keyword">private</span> String sex;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">()</span> &#123;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">User</span><span class="hljs-params">(String name, Integer age, String sex)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.age = age;<br>        <span class="hljs-built_in">this</span>.sex = sex;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">getAge</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> age;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setAge</span><span class="hljs-params">(Integer age)</span> &#123;<br>        <span class="hljs-built_in">this</span>.age = age;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getSex</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> sex;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setSex</span><span class="hljs-params">(String sex)</span> &#123;<br>        <span class="hljs-built_in">this</span>.sex = sex;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;User&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, age=&quot;</span> + age +<br>                <span class="hljs-string">&quot;, sex=&#x27;&quot;</span> + sex + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>创建数据，添加到文档中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Insert</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//插入文档</span><br>        <span class="hljs-type">IndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>();<br>        <span class="hljs-comment">//设置索引及唯一标识</span><br>        request.index(<span class="hljs-string">&quot;users&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>);<br>        <span class="hljs-comment">//创建数据对象</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        user.setName(<span class="hljs-string">&quot;zhangsan&quot;</span>);<br>        user.setAge(<span class="hljs-number">30</span>);<br>        user.setSex(<span class="hljs-string">&quot;男&quot;</span>);<br><br>        <span class="hljs-type">ObjectMapper</span> <span class="hljs-variable">objectMapper</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectMapper</span>();<br>        <span class="hljs-type">String</span> <span class="hljs-variable">userJson</span> <span class="hljs-operator">=</span> objectMapper.writeValueAsString(user);<br><br>        <span class="hljs-comment">//添加文档数据，数据格式为Json格式</span><br>        request.source(userJson, XContentType.JSON);<br><br>        <span class="hljs-comment">//客户端发送请求，获取响应对象</span><br>        <span class="hljs-type">IndexResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.index(request, RequestOptions.DEFAULT);<br>        System.out.println(response.getResult());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/5f7be21c41254bdd908daf15387980af.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="2-修改文档"><a href="#2-修改文档" class="headerlink" title="2.修改文档"></a>2.修改文档</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Update</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//修改文档</span><br>        <span class="hljs-type">UpdateRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UpdateRequest</span>();<br>        <span class="hljs-comment">//设置索引及唯一标识</span><br>        request.index(<span class="hljs-string">&quot;users&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>);<br>        <span class="hljs-comment">//设置请求体，对数据进行修改</span><br>        request.doc(XContentType.JSON,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;女&quot;</span>);<br><br>        <span class="hljs-comment">//客户端发送请求，获取响应对象</span><br>        <span class="hljs-type">UpdateResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.update(request, RequestOptions.DEFAULT);<br>        System.out.println(response.getResult());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/d759d5521feb4c048316b17edc202c36.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="3-查询文档"><a href="#3-查询文档" class="headerlink" title="3.查询文档"></a>3.查询文档</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Get</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-type">GetRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">GetRequest</span>();<br>        request.index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>);<br>        GetResponse response= client.get(request, RequestOptions.DEFAULT);<br>        System.out.println(response.getSourceAsString());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="操作结果："><a href="#操作结果：" class="headerlink" title="操作结果："></a>操作结果：</h5><p><img src="https://img-blog.csdnimg.cn/19e6febb5d2a4244b268fb58d6c1eb57.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="4-删除文档"><a href="#4-删除文档" class="headerlink" title="4.删除文档"></a>4.删除文档</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Delete</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-type">DeleteRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteRequest</span>();<br>        request.index(<span class="hljs-string">&quot;users&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>);<br>        <span class="hljs-type">DeleteResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.delete(request, RequestOptions.DEFAULT);<br>        System.out.println(response.toString());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/127a88508c27425bb7759ba748c8f04e.png" srcset="/blog/img/loading.gif" lazyload></p>
<h5 id="5-批量操作"><a href="#5-批量操作" class="headerlink" title="5.批量操作"></a>5.批量操作</h5><ul>
<li>批量新增</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Insert_batch</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//批量插入文档</span><br>        <span class="hljs-type">BulkRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BulkRequest</span>();<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;zhangsan&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">30</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;男&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1002&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;lisi&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">40</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;男&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1003&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;wangwu&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">30</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;男&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1004&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;wangwu1&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">30</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;女&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1005&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;wangwu2&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">40</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;女&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1006&quot;</span>).source(XContentType.JSON,<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;wangwu3&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-number">50</span>,<span class="hljs-string">&quot;sex&quot;</span>,<span class="hljs-string">&quot;男&quot;</span>));<br>        <span class="hljs-type">BulkResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.bulk(request, RequestOptions.DEFAULT);<br>        System.out.println(response.getTook());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>操作结果：</p>
<p><img src="https://img-blog.csdnimg.cn/658ae883a049406f996a87ecc8f22a2b.png" srcset="/blog/img/loading.gif" lazyload></p>
<ul>
<li>批量删除</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ESTest_Doc_Delete_batch</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">//创建ES客户端</span><br>        <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>                RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>        <span class="hljs-comment">//批量插入文档</span><br>        <span class="hljs-type">BulkRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BulkRequest</span>();<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1001&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1002&quot;</span>));<br>        request.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteRequest</span>().index(<span class="hljs-string">&quot;user&quot;</span>).id(<span class="hljs-string">&quot;1003&quot;</span>));<br>        <span class="hljs-type">BulkResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.bulk(request, RequestOptions.DEFAULT);<br>        System.out.println(response.getTook());<br>        <span class="hljs-comment">//关闭客户端连接</span><br>        client.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h5 id="2-2-5-5-高级查询"><a href="#2-2-5-5-高级查询" class="headerlink" title="2.2.5.5 高级查询"></a>2.2.5.5 高级查询</h5><h5 id="1-查询所有索引数据"><a href="#1-查询所有索引数据" class="headerlink" title="1.查询所有索引数据"></a>1.查询所有索引数据</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 1.全量查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryAll</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//1.全量查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;users&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//查询所有数据</span><br>    sourceBuilder.query(QueryBuilders.matchAllQuery());<br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="2-term-查询，查询条件为关键字"><a href="#2-term-查询，查询条件为关键字" class="headerlink" title="2.term 查询，查询条件为关键字"></a>2.term 查询，查询条件为关键字</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 2.条件查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByCondition</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br><br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br>    <span class="hljs-comment">//2.条件查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br><br>    sourceBuilder.query(QueryBuilders.termQuery(<span class="hljs-string">&quot;age&quot;</span>, <span class="hljs-number">30</span>));<br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-分页查询"><a href="#3-分页查询" class="headerlink" title="3.分页查询"></a>3.分页查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 3.分页查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByPage</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br>     <span class="hljs-comment">//3.分页查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//查询所有数据</span><br>    sourceBuilder.query(QueryBuilders.matchAllQuery());<br><br>    <span class="hljs-comment">//分页查询，从第几页开始查</span><br>    sourceBuilder.from(<span class="hljs-number">2</span>);<br>    <span class="hljs-comment">//每页显示多少条</span><br>    sourceBuilder.size(<span class="hljs-number">2</span>);<br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="4-数据排序"><a href="#4-数据排序" class="headerlink" title="4.数据排序"></a>4.数据排序</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 4.排序</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryBySort</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br>    <span class="hljs-comment">//4.排序</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//查询所有数据</span><br>    sourceBuilder.query(QueryBuilders.matchAllQuery());<br><br>    sourceBuilder.sort(<span class="hljs-string">&quot;age&quot;</span>, SortOrder.DESC);<br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="5-过滤字段"><a href="#5-过滤字段" class="headerlink" title="5.过滤字段"></a>5.过滤字段</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 5.过滤字段</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByFilter</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//5.过滤字段</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//查询所有数据</span><br>    sourceBuilder.query(QueryBuilders.matchAllQuery());<br><br>    <span class="hljs-comment">//排除</span><br>    String[] excludes = &#123;<span class="hljs-string">&quot;age&quot;</span>&#125;;<br>    <span class="hljs-comment">//包括</span><br>    String[] includes = &#123;&#125;;<br><br>    sourceBuilder.fetchSource(includes, excludes);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="6-Bool-查询"><a href="#6-Bool-查询" class="headerlink" title="6.Bool 查询"></a>6.Bool 查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 6.组合查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByConditionGroup</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>     <span class="hljs-comment">//6.组合查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//bool查询构建</span><br>    <span class="hljs-type">BoolQueryBuilder</span> <span class="hljs-variable">boolQueryBuilder</span> <span class="hljs-operator">=</span> QueryBuilders.boolQuery();<br>    <span class="hljs-comment">//必须包含</span><br>    boolQueryBuilder.must(QueryBuilders.matchQuery(<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-string">&quot;30&quot;</span>));<br>    <span class="hljs-comment">//一定不含</span><br>    boolQueryBuilder.mustNot(QueryBuilders.matchQuery(<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;zhangsan&quot;</span>));<br>    <span class="hljs-comment">//可能包含</span><br>    boolQueryBuilder.should(QueryBuilders.matchQuery(<span class="hljs-string">&quot;sex&quot;</span>, <span class="hljs-string">&quot;男&quot;</span>));<br>    sourceBuilder.query(boolQueryBuilder);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="7-范围查询"><a href="#7-范围查询" class="headerlink" title="7.范围查询"></a>7.范围查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 7.范围查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByRange</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//7.f范围查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br>    <span class="hljs-comment">//范围查询构建</span><br>    <span class="hljs-type">RangeQueryBuilder</span> <span class="hljs-variable">rangeQuery</span> <span class="hljs-operator">=</span> QueryBuilders.rangeQuery(<span class="hljs-string">&quot;age&quot;</span>);<br>    <span class="hljs-comment">//大于等于</span><br>    rangeQuery.gte(<span class="hljs-number">30</span>);<br>    <span class="hljs-comment">//小于等于</span><br>    <span class="hljs-comment">//rangeQuery.lte(40);</span><br>    <span class="hljs-comment">//小于不等于</span><br>    rangeQuery.lt(<span class="hljs-number">40</span>);<br><br>    sourceBuilder.query(rangeQuery);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="8-模糊查询"><a href="#8-模糊查询" class="headerlink" title="8.模糊查询"></a>8.模糊查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 8.模糊查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByLike</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//8.模糊查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br><br>    <span class="hljs-comment">//模糊查询 fuzzyQuery, Fuzziness.ONE 模糊字符控制在1个以内</span><br>    <span class="hljs-type">FuzzyQueryBuilder</span> <span class="hljs-variable">fuzzyQueryBuilder</span> <span class="hljs-operator">=</span> QueryBuilders.fuzzyQuery(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;wangw&quot;</span>).fuzziness(Fuzziness.TWO);<br><br>    sourceBuilder.query(fuzzyQueryBuilder);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br><br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="9-高亮查询"><a href="#9-高亮查询" class="headerlink" title="9.高亮查询"></a>9.高亮查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 9.高亮查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByHighlight</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br>    <span class="hljs-comment">//9.高亮查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br><br>    <span class="hljs-comment">//构建查询</span><br>    <span class="hljs-type">TermQueryBuilder</span> <span class="hljs-variable">termQueryBuilder</span> <span class="hljs-operator">=</span> QueryBuilders.termQuery(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;zhangsan&quot;</span>);<br><br>    <span class="hljs-comment">//构建高亮字段</span><br>    <span class="hljs-type">HighlightBuilder</span> <span class="hljs-variable">highlightBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HighlightBuilder</span>();<br>    <span class="hljs-comment">//前缀标签</span><br>    highlightBuilder.preTags(<span class="hljs-string">&quot;&lt;font color = red&gt;&quot;</span>);<br>    <span class="hljs-comment">//后缀标签</span><br>    highlightBuilder.postTags(<span class="hljs-string">&quot;&lt;/font&gt;&quot;</span>);<br>    <span class="hljs-comment">//高亮字段</span><br>    highlightBuilder.field(<span class="hljs-string">&quot;name&quot;</span>);<br>    <span class="hljs-comment">//设置高亮对象</span><br>    sourceBuilder.highlighter(highlightBuilder);<br><br>    <span class="hljs-comment">//设置查询</span><br>    sourceBuilder.query(termQueryBuilder);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(response);<br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br><br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="10-聚合查询"><a href="#10-聚合查询" class="headerlink" title="10.聚合查询"></a>10.聚合查询</h5><ul>
<li>最大年龄</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 10.聚合查询  最大年龄查询</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByGroup</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//10.聚合查询  最大年龄查询</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br><br>    <span class="hljs-comment">//聚合builder AggregationBuilders</span><br>    <span class="hljs-type">MaxAggregationBuilder</span> <span class="hljs-variable">aggregationBuilder</span> <span class="hljs-operator">=</span> AggregationBuilders.max(<span class="hljs-string">&quot;maxAge&quot;</span>).field(<span class="hljs-string">&quot;age&quot;</span>);<br>    sourceBuilder.aggregation(aggregationBuilder);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(response);<br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br><br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="11-分组查询"><a href="#11-分组查询" class="headerlink" title="11.分组查询"></a>11.分组查询</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 11.分组查询  年龄分组</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">queryByGroupBy</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-comment">//创建ES客户端</span><br>    <span class="hljs-type">RestHighLevelClient</span> <span class="hljs-variable">client</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(<br>            RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-number">9200</span>)));<br><br>    <span class="hljs-comment">//11.分组查询  年龄分组</span><br>    <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>();<br>    request.indices(<span class="hljs-string">&quot;user&quot;</span>);<br>    <span class="hljs-comment">//构建SearchSourceBuilder</span><br>    <span class="hljs-type">SearchSourceBuilder</span> <span class="hljs-variable">sourceBuilder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchSourceBuilder</span>();<br><br>    <span class="hljs-comment">//分组builder AggregationBuilders 分组terms</span><br>    <span class="hljs-type">TermsAggregationBuilder</span> <span class="hljs-variable">aggregationBuilder</span> <span class="hljs-operator">=</span> AggregationBuilders.terms(<span class="hljs-string">&quot;ageGroup&quot;</span>).field(<span class="hljs-string">&quot;age&quot;</span>);<br>    sourceBuilder.aggregation(aggregationBuilder);<br><br>    <span class="hljs-comment">//请求source</span><br>    request.source(sourceBuilder);<br><br>    <span class="hljs-type">SearchResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> client.search(request, RequestOptions.DEFAULT);<br><br>    <span class="hljs-comment">//获取所有hits</span><br>    <span class="hljs-type">SearchHits</span> <span class="hljs-variable">hits</span> <span class="hljs-operator">=</span> response.getHits();<br><br>    System.out.println(response);<br>    System.out.println(hits.getTotalHits());<br>    System.out.println(response.getTook());<br><br>    <span class="hljs-comment">//遍历输出hits</span><br>    <span class="hljs-keyword">for</span> (SearchHit hit : hits) &#123;<br>        System.out.println(hit.getSourceAsString());<br>    &#125;<br><br>    <span class="hljs-comment">//关闭客户端连接</span><br>    client.close();<br>&#125;<br></code></pre></td></tr></table></figure>






                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="category-chain-item">学习笔记</a>
  
  

      </span>
    
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E5%BF%85%E5%A4%87%E6%8A%80%E8%83%BD/" class="category-chain-item">必备技能</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/Elasticsearch/">#Elasticsearch</a>
      
        <a href="/blog/tags/%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/">#搜索引擎</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>第二章 ElasticSearch 入门</div>
      <div>https://zqystudy.gitee.io/blog/2022/11/25/第二章 ElasticSearch入门/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Vincent</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年11月25日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2022年11月25日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2022/11/03/%E5%86%85%E9%83%A8%E7%B1%BB/" title="内部类">
                        <span class="hidden-mobile">内部类</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  <article id="comments">
    
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.4.17/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"zc2OPBTVjL4nIWcxAdu9fkZp-gzGzoHsz","appKey":"vpIXqlueEoj5yt2RfIa84WzG","path":"window.location.pathname","placeholder":null,"avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


  </article>


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <footer class="text-center mt-5 py-3">
  
  <!-- 添加网站运行时间 -->
  <div class="footer-content">
    <div>
      
      <script>
      var now = new Date();
      function createtime(){
          var grt= new Date("07/26/2022 09:00:00");//此处修改你的建站时间或者网站上线时间
          now.setTime(now.getTime()+250);
          days = (now - grt ) / 1000 / 60 / 60 / 24;
          dnum = Math.floor(days);
          hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum);
          hnum = Math.floor(hours);
          if(String(hnum).length ==1 ){
              hnum = "0" + hnum;
          }
          minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
          mnum = Math.floor(minutes);
          if(String(mnum).length ==1 ){
                    mnum = "0" + mnum;
          }
          seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
          snum = Math.round(seconds);
          if(String(snum).length ==1 ){
                    snum = "0" + snum;
          }
          document.getElementById("timeDate").innerHTML = "🚀 &nbsp"+dnum+"&nbsp天";  //此次自定义显示内容
          document.getElementById("times").innerHTML = hnum + "&nbsp小时&nbsp" + mnum + "&nbsp分&nbsp" + snum + "&nbsp秒";
      }  //此次自定义显示内容
      setInterval("createtime()",250);
      </script>
    </div>
  </div>
</footer>






<div class="footer-inner">
  
    <div class="footer-content">
       <a href="" target="_blank" rel="nofollow noopener"><span>Vincent</span></a> <a><span>の</span></a> <a href="" target="_blank" rel="nofollow noopener"><span>Blog</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/js/duration.js"></script> </div> 
    </div>
   
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>




  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    
      <script  src="/blog/js/img-lazyload.js" ></script>
    
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/blog/js/leancloud.js" ></script>

  <script  src="/blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
